package net.huashitong.question.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.security.SysPrincipal;
import com.sirdc.modules.sys.service.SysTableService;

import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import net.huashitong.question.dao.CoordinateDao;
import net.huashitong.question.entity.Coordinate;
import net.huashitong.question.filter.CoordinateFilter;
import net.huashitong.taskjob.service.ResDpteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//问题处理
@Service
public class CoordinateService extends StringPKService<Coordinate> {

    @Autowired
    private CoordinateDao dao;
    @Autowired
    SysTableService tableService;
    @Autowired
    private TertiaryIndustryService tertiaryIndustryService;
    @Autowired
    private ResDpteService resDpteService;


    @Override
    protected BaseDao<Coordinate, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(Coordinate obj) {
        obj.setSysId(tableService.updateMaxSysId("t_question", null));
    }

    public List<Coordinate> queryAll(CoordinateFilter filter) {
        return dao.queryAllCoordinate(filter);
    }

//条件搜索

    public List<Coordinate> queryCoordinate(CoordinateFilter filter) {
        return dao.queryCoordinate(filter);
    }

    //保存创建问题协调
    public void saveProblemTask(CoordinateFilter filter){
        //获取指标名称
        if(StringUtils.isNotBlank(filter.getIndustryId())){
            TertiaryIndustry industry = tertiaryIndustryService.getById(filter.getIndustryId());
            filter.setIndustryName(industry.getIndicatorName());
        }
        //获取责任单位名称
        if(StringUtils.isNotBlank(filter.getDeptId())){
            SysDept sysDept = resDpteService.getById(filter.getDeptId());
            filter.setDeptName(sysDept.getDeptName());
        }
        //获取督查组
        if(StringUtils.isNotBlank(filter.getGroupCode())){
            SysDept sysDept = resDpteService.getById(filter.getGroupCode());
            filter.setGroupName(sysDept.getDeptName());
        }
        //上报人信息
        SysPrincipal bnpUser = SysUserUtils.getSysPrincipal();
        filter.setUserName(bnpUser.getName());
        filter.setAaa001("1");
        filter.setSysId(null);
        Coordinate coordinate = new Coordinate();
        BeanUtils.copyProperties(filter, coordinate);
        save(coordinate);
    }
}
